package top.atcra.rpc.transport.message;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

@AllArgsConstructor
@Getter
public enum RPCResponseCode {
    SUCCESS((byte) 20),
    HEARTBEAT_SUCCESS((byte) 21),
    FAIL((byte) 40),
    LIMIT((byte) 41),
    CLOSING((byte) 42),
    METHOD_NOT_FOUND((byte) 44);
    private final byte code;
    private static final Map<Byte, RPCResponseCode> map = new HashMap<>() {{
        for (RPCResponseCode code : RPCResponseCode.values()) {
            put(code.getCode(), code);
        }
    }};

    public static RPCResponseCode valueOf(byte code) {
        return map.get(code);
    }

    @Override
    public String toString() {
        switch (this) {
            case SUCCESS -> {
                return "方法调用成功";
            }
            case HEARTBEAT_SUCCESS -> {
                return "心跳检测成功";
            }
            case FAIL -> {
                return "方法调用失败";
            }
            case LIMIT -> {
                return "方法调用限流";
            }
            case CLOSING -> {
                return "目标服务器正在关闭中";
            }
            case METHOD_NOT_FOUND -> {
                return "未找到目标方法";
            }
        }
        return super.toString();
    }
}